<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多新闻了"
      @load="onLoad"
    >
      <van-card
        v-for="(item, index) in newsList"
        :key="index"
        :title="item.title"
        :thumb="item.img"
        :desc="item.subtitle"
        :num="item.count"
        @click="onDetail(item.id)"
      />
    </van-list>
  </div>
</template>

<script>
import { getNewsListAPI } from "@/api/news.js";

export default {
  data() {
    return {
      newsList: [],
      loading: false,
      finished: false,
      pageIndex: 1,
      pageSize: 10,
    };
  },
  created() {
    this.getNewsList();
  },
  methods: {
    getNewsList() {
      this.loading = true;
      getNewsListAPI({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      }).then((result) => {
        if (result.data.length > 0) {
          this.newsList = this.newsList.concat(result.data);
          this.pageIndex++;
        } else {
          this.finished = true;
        }
        this.loading = false;
      });
    },
    onLoad() {
      this.getNewsList();
    },
    onDetail(id) {
      this.$router.push({
        name: "newsDetail",
        params: { id },
      });
    },
  },
};
</script>

<style scoped>
.van-card {
  margin-bottom: 10px;
}
</style>